package com.funtl.screw.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.dto.UserDTO;
import com.funtl.screw.admin.entity.SysUser;
import com.funtl.screw.admin.vo.UserInfoVo;
import com.funtl.screw.admin.vo.UserVO;
import com.funtl.screw.admin.service.SysUserService;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import com.funtl.screw.common.security.annotation.Inner;
import com.funtl.screw.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Lusifer
 * @since 2018/12/16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理模块")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @Inner
    @GetMapping("/info/{username}")
    public ResponseResult<UserInfoVo> info(@PathVariable String username) {
        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
        if (user == null) {
            return ResponseResult.failed("用户不存在");
        }
        return ResponseResult.ok(userService.findUserInfo(user));
    }

    @Inner
    @ApiIgnore
    @GetMapping("/getUserInfoByMobile/{mobile}")
    public ResponseResult<UserInfoVo> getUserInfoByMobile(@PathVariable String mobile) {
        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getPhone, mobile));
        if (user == null) {
            return ResponseResult.failed(null, String.format("用户信息为空 %s", mobile));
        }
        return ResponseResult.ok(userService.findUserInfo(user));
    }

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping(value = {"/info"})
    public ResponseResult<UserInfoVo> info() {
        String username = SecurityUtils.getUser().getUsername();
        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
        if (user == null) {
            return ResponseResult.failed("用户信息为空");
        }
        return ResponseResult.ok(userService.findUserInfo(user));
    }

    /**
     * 通过ID查询用户信息
     *
     * @param id ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public ResponseResult<UserVO> user(@PathVariable Long id) {
        return ResponseResult.ok(userService.selectUserVoById(id));
    }

    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/details/{username}")
    public ResponseResult<SysUser> user(@PathVariable String username) {
        SysUser condition = new SysUser();
        condition.setUsername(username);
        return ResponseResult.ok(userService.getOne(new QueryWrapper<>(condition)));
    }

    /**
     * 删除用户信息
     *
     * @param id ID
     * @return R
     */
    @SysLog("删除用户信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_user_del')")
    @ApiOperation(value = "删除用户", notes = "根据ID删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
    public ResponseResult<Boolean> userDel(@PathVariable Long id) {
        SysUser sysUser = userService.getById(id);
        return ResponseResult.ok(userService.deleteUserById(sysUser));
    }

    /**
     * 添加用户
     *
     * @param userDto 用户信息
     * @return success/false
     */
    @SysLog("添加用户")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_user_add')")
    public ResponseResult<Boolean> user(@RequestBody UserDTO userDto) {
        return ResponseResult.ok(userService.saveUser(userDto));
    }

    /**
     * 更新用户信息
     *
     * @param userDto 用户信息
     * @return R
     */
    @SysLog("更新用户信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_user_edit')")
    public ResponseResult<Boolean> updateUser(@Valid @RequestBody UserDTO userDto) {
        return ResponseResult.ok(userService.updateUser(userDto));
    }

    /**
     * 分页查询用户
     *
     * @param page    参数集
     * @param userDTO 查询参数列表
     * @return 用户集合
     */
    @GetMapping("/page")
    public ResponseResult<IPage<UserVO>> getUserPage(Page page, UserDTO userDTO) {
        return ResponseResult.ok(userService.getUsersWithRolePage(page, userDTO));
    }

    /**
     * 修改个人信息
     *
     * @param userDto userDto
     * @return success/false
     */
    @SysLog("修改个人信息")
    @PutMapping("/edit")
    public ResponseResult<Boolean> updateUserInfo(@Valid @RequestBody UserDTO userDto) {
        return userService.updateUserInfo(userDto);
    }

    /**
     * @param username 用户名称
     * @return 上级部门用户列表
     */
    @GetMapping("/ancestor/{username}")
    public ResponseResult<List<SysUser>> listAncestorUsers(@PathVariable String username) {
        return ResponseResult.ok(userService.listAncestorUsers(username));
    }

    /**
     * 锁定指定用户
     *
     * @param username 用户名
     * @return R
     */
    @Inner
    @PutMapping("/lock/{username}")
    public ResponseResult<Boolean> lockUser(@PathVariable String username) {
        return userService.lockUser(username);
    }

}
